package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import operations.Lifo;

import languages.Language;

import components.Arc;
import components.Coordinate;
import components.Node;

public class DialogEditArc extends JDialog implements ActionListener {
	private Arc arc;
	private Window parent;
	private Language lang;
	
	private JTextField sLat,sLon;
	private JTextField eLat,eLon;
	private JTextArea breakCords;
	private JTextField three,four,five;
	
	private boolean degrees;
	private JCheckBox type;
	
	public DialogEditArc(Window parent, Language lang, Arc arc) {
		super(parent, lang.dialogEditArcTitle(), true);
		
		this.parent = parent;
		this.lang = lang;
		this.arc = arc;
		this.degrees = true;
		
		this.getContentPane().add(createGUI());
		

		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		this.pack();
		
		this.setVisible(true);
	}

	private JPanel createGUI() {
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		JPanel panCord = new JPanel();
		panCord.setLayout(new GridLayout(2,2));

		panCord.add(new JLabel(lang.tiASLat()));
		sLat = new JTextField(""+arc.getStartNode().getLattitude());
		panCord.add(sLat);
		
		panCord.add(new JLabel(lang.tiASLon()));
		sLon = new JTextField(""+arc.getStartNode().getLongtitude());
		panCord.add(sLon);
		
		panCord.add(new JLabel(lang.tiAELat()));
		eLat = new JTextField(""+arc.getEndNode().getLattitude());
		panCord.add(eLat);
		
		panCord.add(new JLabel(lang.tiAELon()));
		eLon = new JTextField(""+arc.getEndNode().getLongtitude());
		panCord.add(eLon);
		
		panel.add(panCord);
		
		panel.add(new JLabel(lang.dialogEditArcType()));
		type = new JCheckBox();
		type.setSelected(false);
		type.addActionListener(this);
		type.setActionCommand("CHANGE");
		panel.add(type);
		
		panel.add(new JLabel(lang.tiA3()));
		three = new JTextField(""+arc.getThree());
		panel.add(three);
		
		panel.add(new JLabel(lang.tiA4()));
		four = new JTextField(""+arc.getFour());
		panel.add(four);
		
		panel.add(new JLabel(lang.tiA5()));
		five = new JTextField(""+arc.getFive());
		panel.add(five);
		
		this.add(new JLabel(lang.tiBreakCords()));
		breakCords = new JTextArea();
		for(int i=0;i<arc.getBreakCount(); i++){
			breakCords.append(""+arc.getBreakCords().get(i).getLongtitude()+", "+arc.getBreakCords().get(i).getLattitude()+"\n");
		}
		breakCords.setRows(4);
		this.add(breakCords);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BorderLayout());
		JButton add = new JButton(lang.tiAdd());
		JButton clear = new JButton(lang.tiClear());
		buttonPanel.add(add,BorderLayout.WEST);
		buttonPanel.add(clear,BorderLayout.EAST);
		
		add.setActionCommand("ADD");
		clear.setActionCommand("CLEAR");
		add.addActionListener(this);
		clear.addActionListener(this);
		
		panel.add(buttonPanel);
		
		return panel;
		
	}
	
	private void saveModyfication(){
		ArrayList<Coordinate> bC = new ArrayList<Coordinate>();
		
		if(degrees){
			arc.getStartNode().setLattitude(Float.parseFloat(this.sLat.getText()));
			arc.getStartNode().setLongtitude(Float.parseFloat(this.sLon.getText()));
			arc.getEndNode().setLattitude(Float.parseFloat(this.eLat.getText()));
			arc.getEndNode().setLongtitude(Float.parseFloat(this.eLon.getText()));
			
			String[] cords = this.breakCords.getText().split("\n");
			for(int i=0; i< cords.length; i++){
				if(!cords[i].isEmpty()){
					String[] tmp = cords[i].split(",");
					boolean isLon = false;
					Coordinate c = new Coordinate();
					for(int j=0; j<tmp.length; j++){
						if(!tmp[j].isEmpty()){
							if(!isLon){
								c.setLongtitude(Float.parseFloat(tmp[j].trim()));
								isLon = true;
							}
							else{
								c.setLattitude(Float.parseFloat(tmp[j].trim()));
								break;
							}
						}
					}
					bC.add(c);
				}
			}
		}
		else{
			arc.getStartNode().setLattitude(degree(Integer.parseInt(this.sLat.getText())));
			arc.getStartNode().setLongtitude(degree(Integer.parseInt(this.sLon.getText())));
			arc.getEndNode().setLattitude(degree(Integer.parseInt(this.eLat.getText())));
			arc.getEndNode().setLongtitude(degree(Integer.parseInt(this.eLon.getText())));
			
			String[] cords = this.breakCords.getText().split("\n");
			for(int i=0; i< cords.length; i++){
				if(!cords[i].isEmpty()){
					String[] tmp = cords[i].split(",");
					boolean isLon = false;
					Coordinate c = new Coordinate();
					for(int j=0; j<tmp.length; j++){
						if(!tmp[j].isEmpty()){
							if(!isLon){
								c.setLongtitude(degree(Integer.parseInt(tmp[j].trim())));
								isLon = true;
							}
							else{
								c.setLattitude(degree(Integer.parseInt(tmp[j].trim())));
								break;
							}
						}
					}
					bC.add(c);
				}
			}
		}
		
		arc.setThree(Integer.parseInt(three.getText()));
		arc.setFour(Integer.parseInt(four.getText()));
		arc.setFive(Integer.parseInt(five.getText()));
		
		arc.setBreakCount(bC.size());
		arc.setBreakCords(bC);
		
		parent.getArcLayer().changeSelectedGraphic(arc);
		parent.getSW().addNewEvent(new Lifo(arc, Lifo.editArc));
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		
		if("ADD".equals(command)){
			saveModyfication();
		}
		else if("CLEAR".equals(command)){
			clearFields();
		}
		else if("CHANGE".equals(command)){
			if(this.type.isSelected()){
				if(degrees){
					changeTypeToMiliseconds();
				}
			}
			else{
				if(!degrees){
					changeTypeToDerees();
				}
			}
		}
		
	}
	
	private void clearFields(){
		this.degrees = true;
		this.type.setSelected(false);
		this.sLat.setText(""+arc.getStartNode().getLattitude());
		this.sLon.setText(""+arc.getStartNode().getLongtitude());
		this.eLat.setText(""+arc.getEndNode().getLattitude());
		this.eLon.setText(""+arc.getEndNode().getLongtitude());
		this.three.setText(""+arc.getThree());
		this.four.setText(""+arc.getFour());
		this.five.setText(""+arc.getFive());
		for(int i=0;i<arc.getBreakCount(); i++){
			breakCords.append(""+arc.getBreakCords().get(i).getLongtitude()+", "+arc.getBreakCords().get(i).getLattitude()+"\n");
		}
	}
	
	private void changeTypeToMiliseconds() {
		this.sLat.setText(""+milisecond(Float.parseFloat(this.sLat.getText())));
		this.sLon.setText(""+milisecond(Float.parseFloat(this.sLon.getText())));
		this.eLat.setText(""+milisecond(Float.parseFloat(this.eLat.getText())));
		this.eLon.setText(""+milisecond(Float.parseFloat(this.eLon.getText())));
		
		this.breakCords.setText("");
		String[] cords = this.breakCords.getText().split("\n");
		for(int i=0; i< cords.length; i++){
			if(!cords[i].isEmpty()){
				String[] tmp = cords[i].split(",");
				boolean isLon = false;
				for(int j=0; j<tmp.length; j++){
					if(!tmp[j].isEmpty()){
						breakCords.append(""+milisecond(Float.parseFloat(tmp[j].trim())));
						if(!isLon){
							breakCords.append(",");
							isLon = true;
						}
						else{
							breakCords.append("\n");
							break;
						}
					}
				}
			}
		}
		
		degrees = false;
	}
	
	private void changeTypeToDerees() {
		this.sLat.setText(""+degree(Integer.parseInt(this.sLat.getText())));
		this.sLon.setText(""+degree(Integer.parseInt(this.sLon.getText())));
		this.eLat.setText(""+degree(Integer.parseInt(this.eLat.getText())));
		this.eLon.setText(""+degree(Integer.parseInt(this.eLon.getText())));
		
		this.breakCords.setText("");
		String[] cords = this.breakCords.getText().split("\n");
		for(int i=0; i< cords.length; i++){
			if(!cords[i].isEmpty()){
				String[] tmp = cords[i].split(",");
				boolean isLon = false;
				for(int j=0; j<tmp.length; j++){
					if(!tmp[j].isEmpty()){
						breakCords.append(""+degree(Integer.parseInt(tmp[j].trim())));
						if(!isLon){
							breakCords.append(",");
							isLon = true;
						}
						else{
							breakCords.append("\n");
							break;
						}
					}
				}
			}
		}
		
		degrees = true;
	}
	
	private int milisecond(float degree){
		return (int)(degree*1000*3600);
	}
	
	private float degree(int milisecond){
		float d = (float) (milisecond/3600.0);
		return d /= 1000;
	}
}
